package com.uenpay.im.ui.old.activity.mycollect;

import android.text.TextUtils;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class CollectListInvokeItem extends HttpInvokeItem {

	public class CollectListInvokeItemResult {
		public int status;
		public String msg;
		public ArrayList<MyCollect> collect_list = new ArrayList<MyCollect>();
	}

	public CollectListInvokeItemResult getOutput() {
		return (CollectListInvokeItemResult) GetResultObject();
	}

	public CollectListInvokeItem(int page) {
		String pattern = APIConfiguration.getBaseUrl()
				+ "Favorite/Favorite/myFavoriteList?pageIndex=" + page + "&pageSize=12&"+ APIConfiguration.getCustIdAndToken();
		SetUrl(pattern);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		CollectListInvokeItemResult result = new CollectListInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONArray jsondata = json.getJSONArray("data");
			if (jsondata != null) {
				for (int i = 0; i < jsondata.length(); i++) {
					result.collect_list.add(deserializeMyCollect(jsondata
							.getJSONObject(i)));
				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}
		return result;
	}


	private MyCollect deserializeMyCollect(JSONObject json) {
		MyCollect collect = new MyCollect();
		collect.favoriteid = json.optInt("favoriteid");
		collect.targetid = json.optInt("targetid");
		collect.title = json.optString("title");
		collect.fcustid = json.optString("fcustid");
		collect.content = json.optString("content");
		collect.imgurl = json.optString("imgurl");
		collect.url = json.optString("url");
		collect.createtime = json.optString("createtime");
		collect.moduleid = json.optInt("moduleid");
		collect.nickname = json.optString("nickname");
		collect.author = json.optString("author");
		if(!TextUtils.isEmpty(collect.createtime) && collect.createtime.length() > 11){
			collect.createtime = collect.createtime.substring(0, 11);
		}
		return collect;
	}

}
